package com.achen.service.impl;

import com.achen.dao.CheckItemDao;
import com.achen.entity.PageResult;
import com.achen.entity.QueryPageBean;
import com.achen.pojo.CheckItem;
import com.achen.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 * @author achen
 * @create 2020-04-07 16:51
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.addCheckItem(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage(); //当前页
        Integer pageSize = queryPageBean.getPageSize(); //每页显示的页码数
        String queryString = queryPageBean.getQueryString(); //查询的条件

        //分页
        PageHelper.startPage(currentPage,pageSize);

        //调用条件查询的Dao
        Page<CheckItem> page = checkItemDao.findByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void delOneCheckItem(Integer id) {
        //判断检查项是否关联检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项已经关联检查组，不可以删除
            throw new RuntimeException("当前的检查项已经关联了检查组，不可以删除");
        }
        //否则可以删除
        checkItemDao.delOneCheckItem(id);
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemDao.editCheckItem(checkItem);
    }

    @Override
    public CheckItem findCheckItemById(Integer id) {
        return checkItemDao.findCheckItemById(id);
    }

    @Override
    public List<CheckItem> findAllCheckItem() {
        return checkItemDao.findAllCheckItem();
    }

}
